using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Data;
using System.Data.SQLite;

namespace WattsUpComponent
{

    //the class dealing with the method triggered by event.
    public static class GlobalVar
    {
        /// <summary>
        /// Global variable that is constant.
        /// </summary>


        /// <summary>
        /// Static value protected by access routine.
        /// </summary>
        static double _globalValue;

        /// <summary>
        /// Access routine for global variable.
        /// </summary>
        public static double GlobalValue
        {
            get
            {
                return _globalValue;
            }
            set
            {
                _globalValue = value;
            }
        }
    }
    public class Datareceiver
    {
       
        
        double da;
        //the method used to store watts value when receiving data from power meter
        public void getwatts(object sender, WuReadingEventArgs e)
        {
            //EDIT this method to store values into the database
            this.da = e.wuData.Watts;
           
                System.Console.WriteLine(" Power  =" + da);


                // Set global integer.
            GlobalVar.GlobalValue = da;

            //EDIT this method to store values into the database
        }
    }


    public static class Program
    {
       // private static int count = 0;
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        
        public static double  mainFunction(int count)
        {
           // int running = 1;
            WattsUp a = new WattsUp();
            double[] data = new double[200];
            bool b;
            if (count == 0) { b = a.Start(); count++; }
           // b = a.Start();//start the meter to send packet to the system
            WuData myRecord = new WuData();// create a instance for storing the packet as a data structure 
            WuReadingEventArgs e = new WuReadingEventArgs(myRecord);//create eventargs for getting the packet
            Datareceiver r = new Datareceiver();//a class instance the class dealing with the method triggered by event
            a.OnWuReading += new WuReadingEventHandler(r.getwatts);//link the event with the method that would be triggered when the event happen
            double da1 = GlobalVar.GlobalValue;  
             a.wu_OnWuReading(myRecord);//link the data structure with the events
            return da1;
        }
    }
}